import { Message, MessageBox } from 'element-ui';
import store from '../store';

const showMessage = Symbol('showMessage');
let messageInstance = null;
class DonMessage {
  success(options, single = true) {
    this[showMessage]('success', options, single);
  }
  warning(options, single = true) {
    this[showMessage]('warning', options, single);
  }
  info(options, single = true) {
    this[showMessage]('info', options, single);
  }
  error(options, single = true) {
    this[showMessage]('error', options, single);
  }

  [showMessage](type, options, single) {
    if (messageInstance && single) {
      messageInstance.close();// 先把原来的关闭
    }
    messageInstance = Message[type](options);// 再创建新的消息
  }
}

export const showModal = (content, tip = '', options = {}, catchReject = false) => {
  Object.prototype.toString.call(options) !== '[object Object]' && (options = {});
  return new Promise((resolve, reject) => {
    const showMobilePage = store.state.app.showMobilePage;
    const {
      confirmButtonText = '确定',
      cancelButtonText = '取消',
      type = 'warning',
      showClose = false,
      iconClass = 'charge-icon',
      customClass = 'del-message del-message-cancel',
      showCancelButton = true,
      center,
      dangerouslyUseHTMLString,
      closeOnClickModal
    } = options;

    MessageBox.confirm(content, '', {
      confirmButtonText,
      cancelButtonText,
      type,
      showClose,
      center: center !== false,
      dangerouslyUseHTMLString: dangerouslyUseHTMLString !== false,
      iconClass,
      customClass: `${customClass}${showMobilePage ? ' mobile-message' : ''}`,
      showCancelButton,
      closeOnClickModal
    }).then(res => {
      resolve(res);
    }).catch(err => catchReject ? reject(err) : false);
  });
};

export default new DonMessage();
